package cn.crm.web.controller.business;

import cn.crm.common.annotation.Anonymous;
import cn.crm.common.annotation.Log;
import cn.crm.common.core.controller.BaseController;
import cn.crm.common.core.domain.AjaxResult;
import cn.crm.common.core.domain.entity.SysUser;
import cn.crm.common.core.page.TableDataInfo;
import cn.crm.common.enums.BusinessType;
import cn.crm.common.utils.poi.ExcelUtil;
import cn.crm.system.domain.pojo.dto.BusinessDto;
import cn.crm.system.domain.pojo.dto.CaseDto;
import cn.crm.system.domain.pojo.dto.SeriesDto;
import cn.crm.system.domain.pojo.vo.BusinessVo;
import cn.crm.system.domain.pojo.vo.CaseVo;
import cn.crm.system.domain.pojo.vo.SeriesAndspecificationVo;
import cn.crm.system.domain.pojo.vo.SeriesVo;
import cn.crm.system.service.ISeriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/series")
@Api(tags = "业务模块表头模块")
public class SeriesController extends BaseController {

    @Autowired
    private ISeriesService seriesService;

    public SeriesController() {
        log.debug("创建控制器对象：seriesController");
    }


    @PreAuthorize("@ss.hasPermi('system:series:add')")
    @PostMapping("/add")
    @ApiOperation("添加表头资料")
    @Log(title = "产品列表", businessType = BusinessType.INSERT)
    public AjaxResult addSeries(@RequestBody SeriesDto seriesDto) {
        log.debug("开始处理添加表头资料的接口");
        Long seriesId = seriesService.insertSeries(seriesDto);
        return AjaxResult.success("添加成功", seriesId);
    }

    @GetMapping("/list")
    @ApiOperation("查询表头资料")
    @PreAuthorize("@ss.hasPermi('system:series:list')")
    @Log(title = "产品列表", businessType = BusinessType.OTHER)
    public TableDataInfo Getserieslist() {
        log.debug("开始处理查询客户资料的接口");
        List<SeriesVo> seriesVos = seriesService.SelectSeries();
        return getDataTable(seriesVos);
    }


    @GetMapping("/listspecification")
    @ApiOperation("查询表头和单身资料")
    @PreAuthorize("@ss.hasPermi('system:series:list')")
    @Log(title = "产品列表", businessType = BusinessType.OTHER)
    public TableDataInfo Getseriesandspecificationlist(SeriesDto seriesDto) {
        log.debug("开始处理查询单头和单身资料的接口");
        startPage();
        List<SeriesAndspecificationVo> seriesAndspecificationVos = seriesService.SelectSeriesspecification(seriesDto);
        return getDataTable(seriesAndspecificationVos);
    }

    @Log(title = "产品列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{crmpictureId}")
    @ApiOperation("删除客户资料")
    @PreAuthorize("@ss.hasPermi('system:series:delete')")
    public AjaxResult delete(@PathVariable Long[] id,
                             @PathVariable Long[] crmpictureId ){
        log.debug("开始删除表单资料的接口");
        //删除表头数据

        int Deleteseries = seriesService.delSeriesspecification(id,crmpictureId);

        if (Deleteseries < 1){
            return AjaxResult.error("删除客户资料失败");
        }
        return AjaxResult.success("删除客户资料成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据系列编号查询了资料")
    @PreAuthorize("@ss.hasPermi('system:series:query')")
    @Log(title = "产品列表", businessType = BusinessType.OTHER)
    public AjaxResult getInfo(@PathVariable Long id){
        log.debug("开始通过id查询系列资料的接口");
        SeriesVo selectseriesbyid = seriesService.selectseriesbyid(id);
        return  success(selectseriesbyid);
    }

    @Log(title = "产品列表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation("修改系列资料")
    @PreAuthorize("@ss.hasPermi('system:series:update')")
    public AjaxResult update( @RequestBody  SeriesDto seriesDto){
        log.debug("开始修改系列资料的接口");
        int i = seriesService.updateSeries(seriesDto);
        if (i < 1){
            return AjaxResult.error("修改客户资料失败");
        }
        return AjaxResult.success("修改客户资料成功");
    }

    @Log(title = "产品列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:series:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response,SeriesDto seriesDto)
    {
        List<SeriesAndspecificationVo> seriesAndspecificationVos = seriesService.SelectSeriesspecification(seriesDto);
        ExcelUtil<SeriesAndspecificationVo> util = new ExcelUtil<SeriesAndspecificationVo>(SeriesAndspecificationVo.class);
        util.exportExcel(response, seriesAndspecificationVos, "表单数据");
    }


}
